/**
 * 
 */
package com.risetek.tft.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.risetek.tft.entity.TMenu;
import com.risetek.tft.service.MenuService;
import com.zlinepay.framework.bean.AjaxJson;
import com.zlinepay.framework.bean.DataGrid;
import com.zlinepay.framework.bean.DataGridJson;

/**
 * @author  
 * 
 */
@Controller
@RequestMapping("/menu")
public class MenuAction extends BaseAction {
	
	
	@RequestMapping(value = "/add.action")
	public String add(Integer id, ModelMap model) {
		TMenu dto = new TMenu();
		if (id != null) {
		}
		model.addAttribute("dto", dto);
		return "menu/edit";
	}
	
	@RequestMapping(value = "/edit.action")
	public String edit(Integer id, ModelMap model) {
		if (id != null) {
			TMenu dto = menuService.findById(id);
			model.addAttribute("dto", dto);
		}
		model.addAttribute("menus", menuService.getMenuExclude(id));
		return "menu/edit";
	}

	@RequestMapping(value = "/list.action")
	@ResponseBody
	public DataGridJson list(DataGrid dataGrid, HttpServletRequest request,
			ModelMap model) {
		StringBuffer hql = new StringBuffer("select new map(t.id as id,t.TMenu.id as _parentId,t.name as name,t.channel as channel,t.ordno as ordno,t.state as status) from TMenu t");
		if(StringUtils.isNotEmpty(dataGrid.getSort())) {
			hql.append(" order by t."+dataGrid.getSort()+" "+dataGrid.getOrder());
		}
		return menuService.getDataGridList(hql.toString());
	}

	@RequestMapping(value = "/save.action")
	@ResponseBody
	public AjaxJson save(@ModelAttribute(value = "dto") TMenu dto,
			BindingResult bindingResult, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		AjaxJson json = new AjaxJson();
			if (dto.getTMenu() != null
					&& dto.getTMenu().getId() != null) {
			} else {
				dto.setTMenu(null);
			}
			if(dto.getId() == null) {
				menuService.save(dto);
			} else {
				TMenu persistent = menuService.findById(dto.getId());
				if(persistent != null) {
					BeanUtils.copyProperties(dto, persistent, new String[]{"id", "addtime", "updatetime", "nlevel", "scort"});
					menuService.update(persistent);
				}
			}
			menuService.executeProcedure("{call showTreeNodes_menu}");
			json.setSuccess(true);
			json.setMsg("保存成功！");
			json.setMsg(getAllErrors(bindingResult.getAllErrors()));
		return json;
	}

	@RequestMapping(value = "/remove.action")
	@ResponseBody
	public AjaxJson del(Integer[] ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			menuService.delete(ids);
			menuService.executeProcedure("{call showTreeNodes_menu}");
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setMsg("删除失败！");
		}
		return ajaxJson;
	}

	@Autowired
	private MenuService menuService;

}
